#include "qttoosgkeycodetranslator.h"
#include <osgGA/GUIEventAdapter>
//
struct KeyMapItem
{
   Qt::Key qt_key;
   osgGA::GUIEventAdapter::KeySymbol osg_key;
};
//
using namespace osgGA;
//
KeyMapItem key_map[]=
{
   {Qt::Key_Escape,GUIEventAdapter::KEY_Escape},
   {Qt::Key_Tab,GUIEventAdapter::KEY_Tab},
   {Qt::Key_Backspace,GUIEventAdapter::KEY_BackSpace},
   {Qt::Key_Return,GUIEventAdapter::KEY_Return},
   {Qt::Key_Enter,GUIEventAdapter::KEY_KP_Enter},
   {Qt::Key_Insert,GUIEventAdapter::KEY_Insert},
   {Qt::Key_Delete,GUIEventAdapter::KEY_Delete},
   {Qt::Key_Pause,GUIEventAdapter::KEY_Pause},
   {Qt::Key_Print,GUIEventAdapter::KEY_Print},
   {Qt::Key_SysReq,GUIEventAdapter::KEY_Sys_Req},
   {Qt::Key_Clear,GUIEventAdapter::KEY_Clear},
   {Qt::Key_Home,GUIEventAdapter::KEY_Home},
   {Qt::Key_End,GUIEventAdapter::KEY_End},
   {Qt::Key_Left,GUIEventAdapter::KEY_Left},
   {Qt::Key_Up,GUIEventAdapter::KEY_Up},
   {Qt::Key_Right,GUIEventAdapter::KEY_Right},
   {Qt::Key_Down,GUIEventAdapter::KEY_Down},
   {Qt::Key_PageUp,GUIEventAdapter::KEY_Page_Up},
   {Qt::Key_PageDown,GUIEventAdapter::KEY_Page_Down},
   {Qt::Key_CapsLock,GUIEventAdapter::KEY_Caps_Lock},
   {Qt::Key_NumLock,GUIEventAdapter::KEY_Num_Lock},
   {Qt::Key_ScrollLock,GUIEventAdapter::KEY_Scroll_Lock},
   {Qt::Key_F1,GUIEventAdapter::KEY_F1},
   {Qt::Key_F2,GUIEventAdapter::KEY_F2},
   {Qt::Key_F3,GUIEventAdapter::KEY_F3},
   {Qt::Key_F4,GUIEventAdapter::KEY_F4},
   {Qt::Key_F5,GUIEventAdapter::KEY_F5},
   {Qt::Key_F6,GUIEventAdapter::KEY_F6},
   {Qt::Key_F7,GUIEventAdapter::KEY_F7},
   {Qt::Key_F8,GUIEventAdapter::KEY_F8},
   {Qt::Key_F9,GUIEventAdapter::KEY_F9},
   {Qt::Key_F10,GUIEventAdapter::KEY_F10},
   {Qt::Key_F11,GUIEventAdapter::KEY_F11},
   {Qt::Key_F12,GUIEventAdapter::KEY_F12},
   {Qt::Key_F13,GUIEventAdapter::KEY_F13},
   {Qt::Key_F14,GUIEventAdapter::KEY_F14},
   {Qt::Key_F15,GUIEventAdapter::KEY_F15},
   {Qt::Key_F16,GUIEventAdapter::KEY_F16},
   {Qt::Key_F17,GUIEventAdapter::KEY_F17},
   {Qt::Key_F18,GUIEventAdapter::KEY_F18},
   {Qt::Key_F19,GUIEventAdapter::KEY_F19},
   {Qt::Key_F20,GUIEventAdapter::KEY_F20},
   {Qt::Key_F21,GUIEventAdapter::KEY_F21},
   {Qt::Key_F22,GUIEventAdapter::KEY_F22},
   {Qt::Key_F23,GUIEventAdapter::KEY_F23},
   {Qt::Key_F24,GUIEventAdapter::KEY_F24},
   {Qt::Key_F25,GUIEventAdapter::KEY_F25},
   {Qt::Key_F26,GUIEventAdapter::KEY_F26},
   {Qt::Key_F27,GUIEventAdapter::KEY_F27},
   {Qt::Key_F28,GUIEventAdapter::KEY_F28},
   {Qt::Key_F29,GUIEventAdapter::KEY_F29},
   {Qt::Key_F30,GUIEventAdapter::KEY_F30},
   {Qt::Key_F31,GUIEventAdapter::KEY_F31},
   {Qt::Key_F32,GUIEventAdapter::KEY_F32},
   {Qt::Key_F33,GUIEventAdapter::KEY_F33},
   {Qt::Key_F34,GUIEventAdapter::KEY_F34},
   {Qt::Key_F35,GUIEventAdapter::KEY_F35},
   {Qt::Key_Menu,GUIEventAdapter::KEY_Menu},
   {Qt::Key_Help,GUIEventAdapter::KEY_Help},
   {Qt::Key_Space,GUIEventAdapter::KEY_Space},
   {Qt::Key_Asterisk,GUIEventAdapter::KEY_KP_Multiply},
   {Qt::Key_Plus,GUIEventAdapter::KEY_KP_Add},
   {Qt::Key_Comma,GUIEventAdapter::KEY_KP_Separator},
   {Qt::Key_Minus,GUIEventAdapter::KEY_KP_Subtract},
   {Qt::Key_Period,GUIEventAdapter::KEY_KP_Decimal},
   {Qt::Key_Slash,GUIEventAdapter::KEY_KP_Divide},
   {Qt::Key_Equal,GUIEventAdapter::KEY_KP_Equal},
   {Qt::Key_Cancel,GUIEventAdapter::KEY_Cancel},
   {Qt::Key_Select,GUIEventAdapter::KEY_Select},
   {Qt::Key_Execute,GUIEventAdapter::KEY_Execute},
   {Qt::Key_Search,GUIEventAdapter::KEY_Find},
   //
   {Qt::Key_Shift,GUIEventAdapter::KEY_Shift_L},
   {Qt::Key_Control,GUIEventAdapter::KEY_Control_L},
   {Qt::Key_Meta,GUIEventAdapter::KEY_Meta_L},
   {Qt::Key_Alt,GUIEventAdapter::KEY_Alt_L},
   {Qt::Key_Super_L,GUIEventAdapter::KEY_Super_L},
   {Qt::Key_Super_R,GUIEventAdapter::KEY_Super_R},
   {Qt::Key_Hyper_L,GUIEventAdapter::KEY_Hyper_L},
   {Qt::Key_Hyper_R,GUIEventAdapter::KEY_Hyper_R}
};
//
QtToOsgKeyCodeTranslator::QtToOsgKeyCodeTranslator()
{
   for(unsigned int i=0;i<sizeof(key_map);++i)
   {
      key_dictionary[key_map[i].qt_key]=key_map[i].osg_key;
   }
}
QtToOsgKeyCodeTranslator::~QtToOsgKeyCodeTranslator()
{
}
//
void QtToOsgKeyCodeTranslator::initialize()
{
   if(!impl)
   {
      impl=new QtToOsgKeyCodeTranslator;
   }
}
void QtToOsgKeyCodeTranslator::uninitialize()
{
   if(impl)
   {
      delete impl; impl=0;
   }
}
QtToOsgKeyCodeTranslator *QtToOsgKeyCodeTranslator::impl=0;
//
void QtToOsgKeyCodeTranslator::qt_key_to_osg_key(int p_qt_key,int &p_osg_key,bool &p_ok)
{
   QHash<int,int>::const_iterator i=impl->key_dictionary.find(p_qt_key);
   p_ok=(i!=impl->key_dictionary.end());
   if(p_ok) p_osg_key=i.value();
   else p_osg_key=0;
}
